﻿// -----------------------------------------------------------------------
// <copyright file="IMG_Algebra.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImageProcessing.IMG_Processor
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class IMG_Algebra
    {
        /// <summary>
        /// IMs the g_ adder.
        /// </summary>
        /// <param name="img_OldBuffer">The img_ old buffer.</param>
        /// <param name="img_Fractoin">The img_ fractoin.</param>
        /// <param name="img_NewBuffer">The img_ new buffer.</param>
        /// <returns>new image buffer</returns>
        public static Entities.IMG_Image IMG_Adder(Entities.IMG_Image img_OldBuffer, float img_Fractoin, Entities.IMG_Image img_NewBuffer)
        {
            try
            {
                Entities.Pixel img_NewPixel;
                Entities.IMG_Image img_temp = new Entities.IMG_Image(img_OldBuffer.Width, img_OldBuffer.Height);
                img_temp = IMG_Processor.IMG_Resize.IMG_BiLinearResize(img_NewBuffer, img_OldBuffer.Width, img_OldBuffer.Height);
                int img_totalLength = img_OldBuffer.Height;
                int img_totalWidth = img_OldBuffer.Width;
                Entities.IMG_Image img_ResultBuffer = new Entities.IMG_Image(img_totalWidth, img_totalLength);
                int index = img_totalWidth * img_totalLength;
                for (int i = 0; i < index; i++)
                {
                    img_NewPixel = new Entities.Pixel(
                        (int)((img_Fractoin * img_OldBuffer.ImagePixels[i].Red) + ((1 - img_Fractoin) * img_temp.ImagePixels[i].Red)),
                        (int)((img_Fractoin * img_OldBuffer.ImagePixels[i].Green) + ((1 - img_Fractoin) * img_temp.ImagePixels[i].Green)),
                        (int)((img_Fractoin * img_OldBuffer.ImagePixels[i].Blue) + ((1 - img_Fractoin) * img_temp.ImagePixels[i].Blue)));
                    img_ResultBuffer.ImagePixels[i] = img_NewPixel;
                }

                return img_ResultBuffer;
            }
            catch (Exception img_ex)
            {
                Logger.LogException(img_ex);
                throw;
            }
        }

        /// <summary>
        /// IMs the g_ NOT.
        /// </summary>
        /// <param name="img_OldBuffer">The img_ old buffer.</param>
        /// <returns>new image buffer</returns>
        public static Entities.IMG_Image IMG_NOT(Entities.IMG_Image img_OldBuffer)
        {
            try
            {
                Entities.Pixel img_NewPixel;
                Entities.IMG_Image img_temp = new Entities.IMG_Image(img_OldBuffer.Width, img_OldBuffer.Height);
                Entities.IMG_Image img_ResultBuffer = new Entities.IMG_Image(img_OldBuffer.Width, img_OldBuffer.Height);
                int index = img_OldBuffer.Width * img_OldBuffer.Height;
                for (int i = 0; i < index; i++)
                {
                    img_NewPixel = new Entities.Pixel(
                        (255 - img_OldBuffer.ImagePixels[i].Red),
                        (255 - img_OldBuffer.ImagePixels[i].Green),
                        (255 - img_OldBuffer.ImagePixels[i].Blue));
                    img_ResultBuffer.ImagePixels[i] = img_NewPixel;
                }

                return img_ResultBuffer;
            }
            catch (Exception img_ex)
            {
                Logger.LogException(img_ex);
                throw;
            }
        }

        /// <summary>
        /// IMs the g_ subtractor.
        /// </summary>
        /// <param name="img_OldBuffer">The img_ old buffer.</param>
        /// <param name="img_NewBuffer">The img_ new buffer.</param>
        /// <returns>new image buffer</returns>
        public static Entities.IMG_Image IMG_Subtractor(Entities.IMG_Image img_OldBuffer, Entities.IMG_Image img_NewBuffer)
        {
            try
            {
                Entities.IMG_Image img_NewImg = new Entities.IMG_Image(img_OldBuffer.Width, img_OldBuffer.Height);

                Entities.IMG_Image img_temp = new Entities.IMG_Image(img_OldBuffer.Width, img_OldBuffer.Height);
                img_temp = IMG_Processor.IMG_Resize.IMG_BiLinearResize(img_NewBuffer, img_OldBuffer.Width, img_OldBuffer.Height);

                int img_Width = img_OldBuffer.Width;
                int img_Height = img_OldBuffer.Height;

                double newR, newG, newB;
                int index = img_Width * img_Height;
                for (int i = 0; i < index; i++)
                {
                    newR = Math.Abs(img_OldBuffer.ImagePixels[i].Red - img_temp.ImagePixels[i].Red);
                    newG = Math.Abs(img_OldBuffer.ImagePixels[i].Green - img_temp.ImagePixels[i].Green);
                    newB = Math.Abs(img_OldBuffer.ImagePixels[i].Blue - img_temp.ImagePixels[i].Blue);
                    img_NewImg.ImagePixels[i] = new Entities.Pixel(newR, newG, newB);
                }

                return img_NewImg;
            }
            catch (Exception img_ex)
            {
                Logger.LogException(img_ex);
                throw;
            }
        }
    }
}
